<?php

namespace App\HttpController\Admin\Auth;

use App\HttpController\Admin\BaseController;

use App\Logic\Auth\Form\MenuRoleSaveForm;
use App\Logic\Auth\Form\RoleListForm;
use App\Logic\Auth\Form\RoleSaveForm;

use App\Logic\Auth\Form\RoleStateSaveForm;
use App\Logic\Auth\RoleService;

use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

/**
 * Class Role
 * @package App\HttpController\Api\Auth
 * @ApiGroup(groupName="权限-角色管理")
 * @ApiGroupDescription("权限-角色管理")
 */
class Role extends BaseController
{

    /**
     * @Api(name="列表",path="/admin/auth/role/index")
     * @Method(allow={GET})
     * @ApiDescription("列表")
     * @ApiSuccessParam(name="id",description="spu id")
     * @ApiSuccessParam(name="name",description="角色")
     * @ApiSuccessParam(name="state",description="状态")
     * @ApiSuccessParam(name="is_action",description="能否操作")
     */
    public function index()
    {
        $params = $this->request()->getQueryParams();

        $listForm = new RoleListForm($params, $this->request());

        $result = $listForm->lists();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="保存",path="/admin/auth/role/action")
     * @ApiDescription("保存")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="id",description="ID值【更新时必填】",type="integer")
     * @Param(name="name",description="角色",required="",type="string")
     * @Param(name="state",description="是否显示",required="",type="integer")
     * @Param(name="type",description="类型",required="",type="integer")
     * @ApiSuccessParam(name="id",description="操作id",type="integer")
     */
    public function action()
    {
        $jsonData = $this->getJson();

        $saveForm = new RoleSaveForm($jsonData);

        $result = $saveForm->saveData();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="删除",path="/admin/auth/role/delete")
     * @ApiDescription("删除记录")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="selected",description="删除的ID",required="",type="array")
     * @ApiSuccessParam(name="affectedRows",description="影响行数",type="integer")
     */
    public function delete()
    {
        $selected = $this->getJson('selected');

        $result = RoleService::getInstance()->deleteRecord($selected);

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="字典",path="/admin/auth/role/dict")
     * @ApiDescription("字典")
     * @ApiSuccessParam(name="type_list",description="供应商类型",type="array")
     * @Method(allow={GET})
     */
    public function dict()
    {
        $result = RoleService::getInstance()->getDict();
        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="角色权限详情",path="/admin/auth/role/info")
     * @ApiDescription("字典")
     * @ApiSuccessParam(name="type_list",description="供应商类型",type="array")
     * @Method(allow={GET})
     */
    public function info()
    {
        $id     = $this->request()->getQueryParam('id');
        $result = RoleService::getInstance()->getInfo($id);
        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="保存权限",path="/admin/auth/role/saveAuth")
     * @ApiDescription("保存权限")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="role_id",description="角色id",type="integer")
     * @Param(name="menu_ids",description="菜单id",required="",type="integer")
     * @ApiSuccessParam(name="id",description="操作id",type="integer")
     */
    public function saveAuth()
    {
        $jsonData = $this->getJson();

        $saveForm = new MenuRoleSaveForm($jsonData);

        $result = $saveForm->saveData();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="设置显示",path="/admin/auth/role/settingState")
     * @ApiDescription("设置显示")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="id",description="角色id",type="integer")
     * @ApiSuccessParam(name="id",description="操作id",type="integer")
     */
    public function settingState()
    {
        $jsonData = $this->getJson();

        $saveForm = new RoleStateSaveForm($jsonData);

        $result = $saveForm->saveData();

        return $this->writeJsonByOutput($result);
    }


}
